﻿using System; 
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic; 
using System.Data;

namespace Sasc.PV.DAL
{
		/// <summary>
	/// PvcGongXu
	/// </summary>
		public partial class PvcGongXu:IPVDomain
	{ 
		#region Fields
	    	private int _id;		
		/// <summary>
		/// ID
        /// </summary>		
        public int ID
        {
            get{ return _id; }
            set{ _id = value; }
        }        
			private string _name;		
		/// <summary>
		/// Name
        /// </summary>		
        public string Name
        {
            get{ return _name; }
            set{ _name = value; }
        }        
			private string _chejianname;		
		/// <summary>
		/// CheJianName
        /// </summary>		
        public string CheJianName
        {
            get{ return _chejianname; }
            set{ _chejianname = value; }
        }        
			private int _pregongxuid;		
		/// <summary>
		/// PreGongXuID
        /// </summary>		
        public int PreGongXuID
        {
            get{ return _pregongxuid; }
            set{ _pregongxuid = value; }
        }        
			private int _tofangonggongxuid;		
		/// <summary>
		/// ToFanGongGongXuID
        /// </summary>		
        public int ToFanGongGongXuID
        {
            get{ return _tofangonggongxuid; }
            set{ _tofangonggongxuid = value; }
        }        
			private int _gongxuorder;		
		/// <summary>
		/// GongXuOrder
        /// </summary>		
        public int GongXuOrder
        {
            get{ return _gongxuorder; }
            set{ _gongxuorder = value; }
        }        
			private string _gongxutablename;		
		/// <summary>
		/// GongXuTableName
        /// </summary>		
        public string GongXuTableName
        {
            get{ return _gongxutablename; }
            set{ _gongxutablename = value; }
        }        
			private int _dependongongxuid;		
		/// <summary>
		/// DependOnGongXuID
        /// </summary>		
        public int DependOnGongXuID
        {
            get{ return _dependongongxuid; }
            set{ _dependongongxuid = value; }
        }        
		   		#endregion
   		
   		#region DAL Methods
		public bool Exists(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from PvcGongXu");
			strSql.Append(" where ");
			                                       strSql.Append(" ID = @ID  ");
                            			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}
		
		
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add()
		{
          return  Add(this);
		}
				
		
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(PvcGongXu model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into PvcGongXu(");			
            strSql.Append("Name,CheJianName,PreGongXuID,ToFanGongGongXuID,GongXuOrder,GongXuTableName,DependOnGongXuID");
			strSql.Append(") values (");
            strSql.Append("@Name,@CheJianName,@PreGongXuID,@ToFanGongGongXuID,@GongXuOrder,@GongXuTableName,@DependOnGongXuID");            
            strSql.Append(") ");            
            strSql.Append(";select @@IDENTITY");		
			SqlParameter[] parameters = {
			            new SqlParameter("@Name", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@CheJianName", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@PreGongXuID", SqlDbType.Int,4) ,            
                        new SqlParameter("@ToFanGongGongXuID", SqlDbType.Int,4) ,            
                        new SqlParameter("@GongXuOrder", SqlDbType.Int,4) ,            
                        new SqlParameter("@GongXuTableName", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@DependOnGongXuID", SqlDbType.Int,4)             
              
            };
			            
            parameters[0].Value = model.Name;                        
            parameters[1].Value = model.CheJianName;                        
            parameters[2].Value = model.PreGongXuID;                        
            parameters[3].Value = model.ToFanGongGongXuID;                        
            parameters[4].Value = model.GongXuOrder;                        
            parameters[5].Value = model.GongXuTableName;                        
            parameters[6].Value = model.DependOnGongXuID;                        
			   
			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);			
			if (obj == null)
			{
				return 0;
			}
			else
			{
				                    
            	return Convert.ToInt32(obj);
                                                                  
			}			   
            			
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update()
		{
			return Update(this);
		}
		
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(PvcGongXu model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update PvcGongXu set ");
			                                                
            strSql.Append(" Name = @Name , ");                                    
            strSql.Append(" CheJianName = @CheJianName , ");                                    
            strSql.Append(" PreGongXuID = @PreGongXuID , ");                                    
            strSql.Append(" ToFanGongGongXuID = @ToFanGongGongXuID , ");                                    
            strSql.Append(" GongXuOrder = @GongXuOrder , ");                                    
            strSql.Append(" GongXuTableName = @GongXuTableName , ");                                    
            strSql.Append(" DependOnGongXuID = @DependOnGongXuID  ");            			
			strSql.Append(" where ID=@ID ");
						
SqlParameter[] parameters = {
			            new SqlParameter("@ID", SqlDbType.Int,4) ,            
                        new SqlParameter("@Name", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@CheJianName", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@PreGongXuID", SqlDbType.Int,4) ,            
                        new SqlParameter("@ToFanGongGongXuID", SqlDbType.Int,4) ,            
                        new SqlParameter("@GongXuOrder", SqlDbType.Int,4) ,            
                        new SqlParameter("@GongXuTableName", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@DependOnGongXuID", SqlDbType.Int,4)             
              
            };
						            
            parameters[0].Value = model.ID;                        
            parameters[1].Value = model.Name;                        
            parameters[2].Value = model.CheJianName;                        
            parameters[3].Value = model.PreGongXuID;                        
            parameters[4].Value = model.ToFanGongGongXuID;                        
            parameters[5].Value = model.GongXuOrder;                        
            parameters[6].Value = model.GongXuTableName;                        
            parameters[7].Value = model.DependOnGongXuID;                        
            int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from PvcGongXu ");
			strSql.Append(" where ID=@ID");
						SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;


			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
				/// <summary>
		/// 批量删除一批数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from PvcGongXu ");
			strSql.Append(" where ID in ("+IDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
				
		
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public PvcGongXu GetModel(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID, Name, CheJianName, PreGongXuID, ToFanGongGongXuID, GongXuOrder, GongXuTableName, DependOnGongXuID  ");			
			strSql.Append("  from PvcGongXu ");
			strSql.Append(" where ID=@ID");
						SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			
			PvcGongXu model=new PvcGongXu();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			
			if(ds.Tables[0].Rows.Count>0)
			{
												if(ds.Tables[0].Rows[0]["ID"].ToString()!="")
				{
					model.ID=int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
				}
																																				model.Name= ds.Tables[0].Rows[0]["Name"].ToString();
																																model.CheJianName= ds.Tables[0].Rows[0]["CheJianName"].ToString();
																												if(ds.Tables[0].Rows[0]["PreGongXuID"].ToString()!="")
				{
					model.PreGongXuID=int.Parse(ds.Tables[0].Rows[0]["PreGongXuID"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["ToFanGongGongXuID"].ToString()!="")
				{
					model.ToFanGongGongXuID=int.Parse(ds.Tables[0].Rows[0]["ToFanGongGongXuID"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["GongXuOrder"].ToString()!="")
				{
					model.GongXuOrder=int.Parse(ds.Tables[0].Rows[0]["GongXuOrder"].ToString());
				}
																																				model.GongXuTableName= ds.Tables[0].Rows[0]["GongXuTableName"].ToString();
																												if(ds.Tables[0].Rows[0]["DependOnGongXuID"].ToString()!="")
				{
					model.DependOnGongXuID=int.Parse(ds.Tables[0].Rows[0]["DependOnGongXuID"].ToString());
				}
																														
				return model;
			}
			else
			{
				return null;
			}
		}
		
		
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM PvcGongXu ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}
		
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" * ");
			strSql.Append(" FROM PvcGongXu ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}
		
		
		        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="pageIndex">从0开始</param>
        /// <param name="pageSize"></param>
        /// <param name="strWhere"></param>
        /// <param name="filedOrder"></param>
        /// <returns></returns>
		public DataSet GetList(int pageIndex,int pageSize, string strWhere  = null,string filedOrder = null)
		{
			var startIndex = pageIndex * pageSize + 1;
			var endIndex = (pageIndex + 1) * pageSize;
			var totalCount = 0;
			
			var totalCountSqlBuilder = new StringBuilder();
			totalCountSqlBuilder.Append(" SELECT COUNT([ID]) FROM PvcGongXu ");
			if(!string.IsNullOrEmpty(strWhere))
			{
				totalCountSqlBuilder.Append(" where "+strWhere);
			}
			totalCount = (int)DbHelperSQL.GetSingle(totalCountSqlBuilder.ToString());
			
				StringBuilder strSql=new StringBuilder();
				strSql.Append("select TOP "+pageSize.ToString()+" t.* from ");
				strSql.Append("(select ");
				strSql.Append(" row_number() over (" );
				if(!string.IsNullOrEmpty(filedOrder))
				{
					strSql.Append(" order by " + filedOrder);
				}
				else 
				{
					strSql.Append(" ORDER BY [ID] ");
				}
				strSql.Append(") AS ___rownum, ");
				
				strSql.Append(" * ");
				strSql.Append(" FROM PvcGongXu ");
				if(!string.IsNullOrEmpty(strWhere))
				{
					strSql.Append(" where "+strWhere);
				}
				strSql.Append(") t ");
				strSql.Append(" where t.___rownum>="+startIndex.ToString()+" AND t.___rownum<="+endIndex.ToString());
				if(!string.IsNullOrEmpty(filedOrder))
				{
					strSql.Append(" order by " + filedOrder);
				}
				
				var dataSet = DbHelperSQL.Query(strSql.ToString());
					
            if (totalCount > 0 && startIndex <= totalCount)
			{		
				for(var i=1;i<startIndex;i++)
				{
					dataSet.Tables[0].Rows.InsertAt(dataSet.Tables[0].NewRow(),0);				
				}
				for(var i=endIndex;i<totalCount;i++)
				{
					dataSet.Tables[0].Rows.Add(dataSet.Tables[0].NewRow());
				}
			}
			return dataSet;
		}
				#endregion
   
   		#region IPVDomain
   		public string GetDbTableName()
   		{
   			return "PvcGongXu";
   		}
   		#endregion
	}
}

